home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / GraphicsWorkshop / Source / Converters / xbm.m < prev   
Encoding:
Text File  |  1992-05-11  |  3.9 KB  |  208 lines

  1. #import <stdio.h>
  2. #import <libc.h>
  3. #import <math.h>
  4. #import <streams/streams.h>
  5. #import <appkit/graphics.h>
  6. #import <appkit/NXBitmapImageRep.h>
  7. #import "NXBitmapImageRepControl.h"
  8. #import "ImageControl.h"
  9. #import "xbm.h"
  10.  
  11. @implementation XBM
  12.  
  13. unsigned char FlipBits(unsigned char inByte);
  14.  
  15. - init
  16. {
  17.     return self;
  18. }
  19.  
  20. - free
  21. {
  22.     return self;
  23. }
  24.  
  25. unsigned char FlipBits(unsigned char inByte)
  26. {
  27.     unsigned char        outByte = 0;
  28.     int                inPos = 0x01, 
  29.                     outPos = 0x80;
  30.                     
  31.     if (!inByte || (inByte == 0xFF)) return inByte;
  32.     while (inPos) {
  33.         if (inByte & inPos) {
  34.             outByte |= outPos;
  35.         }
  36.         inPos <<= 1;
  37.         outPos >>= 1;
  38.     }
  39.     return outByte;
  40. }
  41.  
  42. - readFromStream: (NXStream *)stream from: sender;
  43. {
  44.     id                image;
  45.     char                buffer1[100];
  46.     char                buffer2[100];
  47.     char                buffer3[100];
  48.     char                buffer4[100];
  49.     int                width = 0, height = 0;
  50.     unsigned char        *ptr;
  51.     unsigned char        *size;
  52.     int                tmp;
  53.  
  54. #ifdef DEBUG
  55.     fprintf(stderr, "Made it to read\n");
  56. #endif
  57.  
  58.     do {
  59.         NXScanf(stream, "%99[^\n]", buffer1); NXGetc(stream);
  60.         if (!strncmp(buffer1, "#define", 7)) {
  61.             sscanf(buffer1, "%s %s %s", buffer2, buffer3, buffer4);
  62.             if (!strcmp(buffer3 + strlen(buffer3) - 5, "width")) {
  63.                 width = atoi(buffer4);
  64. #ifdef DEBUG
  65.                 fprintf(stderr, "Width = %d\n", width);
  66. #endif
  67.             }
  68.             else if (!strcmp(buffer3 + strlen(buffer3) - 6, "height")) {
  69.                 height = atoi(buffer4);
  70. #ifdef DEBUG
  71.                 fprintf(stderr, "Height = %d\n", height);
  72. #endif
  73.             }
  74.         }
  75.     } while (strncmp(buffer1, "static", 6));
  76.     
  77.     image = [[NXBitmapImageRep alloc]     initData: NULL
  78.                                         pixelsWide: width
  79.                                         pixelsHigh: height
  80.                                         bitsPerSample: 1
  81.                                         samplesPerPixel: 1
  82.                                         hasAlpha: NO
  83.                                         isPlanar: NO
  84.                                         colorSpace: NX_OneIsBlackColorSpace
  85.                                         bytesPerRow: 0
  86.                                         bitsPerPixel: 0];
  87.     ptr = [image data];
  88.     size = ptr + (((width + 7) / 8) * height);
  89. #ifdef DEBUG
  90.     fprintf(stderr, "%p %p\n", ptr, size);
  91. #endif
  92.     NXGetc(stream);
  93.     do {
  94.         NXScanf(stream, "%[^,]", buffer1); NXGetc(stream);
  95.         sscanf(buffer1, "%x", &tmp);
  96.         *ptr++ = FlipBits(tmp);
  97.     } while (ptr < size);
  98.  
  99.     return image;
  100. }
  101.  
  102. - (BOOL)write: (id)image toStream: (NXStream *)stream from: sender;
  103. {
  104.     id                newImage;
  105.     id                imageCon;
  106.     unsigned char        *dataPtr;
  107.     int                size, x;
  108.     char                *nameBuffer;
  109.     
  110.     imageCon = [sender getImageControl: image];
  111. #ifdef DEBUG
  112.     fprintf(stderr, "1...");
  113. #endif
  114.     newImage = [imageCon ditherImage];
  115. #ifdef DEBUG
  116.     fprintf(stderr, "2...");
  117. #endif
  118.     [imageCon free];
  119. #ifdef DEBUG
  120.     fprintf(stderr, "3...");
  121. #endif
  122.     
  123.     nameBuffer = [sender filename];
  124.     NXPrintf(stream, "#define %s_width %d\n", nameBuffer, [newImage pixelsWide]);
  125.     NXPrintf(stream, "#define %s_height %d\n", nameBuffer, [newImage pixelsHigh]);
  126.     NXPrintf(stream, "static char %s_bits[] = {", nameBuffer);
  127.     size = (([newImage pixelsWide] + 7) / 8)* [newImage pixelsHigh];
  128. #ifdef DEBUG
  129.     fprintf(stderr, "%d\n", size);
  130. #endif
  131.     for (x = 0, dataPtr = [newImage data]; x < size - 1; x++) {
  132.         if (!(x % 12)) NXPrintf(stream, "\n   ");
  133.         NXPrintf(stream, "0x%02X,", FlipBits(*dataPtr++ ^ 0xFF));
  134.     }
  135.     NXPrintf(stream, "0x%02X};\n", FlipBits(*dataPtr ^ 0xFF));
  136.     
  137.     [newImage free];
  138.     
  139.     return YES;
  140. }
  141.  
  142. - readAllFromStream: (NXStream *)stream from: sender
  143. {
  144.     return nil;
  145. }
  146.  
  147. - (BOOL)writeAll: (id)image toStream: (NXStream *)stream
  148. {
  149.     return NO;
  150. }
  151.  
  152. - customSaveView: (int)width
  153. {
  154.     return nil;
  155. }
  156.  
  157. - customOpenView: (int)width
  158. {
  159.     return nil;
  160. }
  161.  
  162. - (char *)getFormatName
  163. {
  164.     return("X11 Portable Bitmap (XBM)");
  165. }
  166.  
  167.  - (BOOL)setCustomParameter: (const char *)parameter withValue: (void *)ptr
  168. {
  169.     return NO;
  170. }
  171.  
  172.  - (void *)getCustomParameter: (const char *)parameter
  173. {
  174.     return nil;
  175. }
  176.  
  177. - (char *)copyrightNotice
  178. {
  179.     return "XBM Converter\nby Alex Raftis\nCopyright (c) 1991 Cal Poly State University\nEmail bugs to alex@data.ACS.CalPoly.EDU";
  180. }
  181.  
  182. - (int)errorState
  183. {
  184.     return CONVERT_ERR_NONE;
  185. }
  186.  
  187.  - (int)errorMessage
  188. {
  189.     return ERROR_NO_ERROR;
  190. }
  191.  
  192. - (char *)errorStringMessage
  193. {
  194.     return NULL;
  195. }
  196.  
  197. - (BOOL)needsWindowServer;
  198. {
  199.     return NO;
  200. }
  201.  
  202. - (char *)protocolVersion
  203. {
  204.     return "1.0";
  205. }
  206.  
  207. @end
  208.